using System;

namespace SanFS.DiscLibrary
{
	public enum DriveState
	{
		Idle,
		Mounting,
		Mounted,
		Unmounting,
		Writing,
		Ejecting,
		Error
	}
	
	public class Drive
	{
		private string			mDev;
		private string			mMountPoint;
		private bool			mMounted = false;
		private DriveState		mState = DriveState.Idle;
		
		public Drive(string Dev, string MountPoint)
		{
			mDev = Dev;
			mMountPoint = MountPoint;
		}
		
		public void Eject()
		{
			mState = DriveState.Ejecting;
			try
			{
				Console.WriteLine("Drive.Eject():");
				CmdWrapper cmd = new CmdWrapper("/usr/bin/eject");
				cmd.Run();
				mState = DriveState.Idle;
			}
			catch
			{
				mState = DriveState.Error;
			}
		}
		
		public void Mount()
		{
			mState = DriveState.Mounting;
			try
			{
				Console.WriteLine("Drive.Mount(): Device '{0}', MntPt '{1}'", mDev, mMountPoint);
				CmdWrapper	cmd = new CmdWrapper("/bin/mount " + mDev + " " + mMountPoint);
				cmd.MergeStderr = true;
				string		[]output = cmd.Run();
			
				foreach (string s in output)
					Console.WriteLine("Drive.Mount(): DBG '{0}'", s);
			
				mState = DriveState.Mounted;
			}
			catch
			{
				mState = DriveState.Idle;
				throw;
			}
		}
		
		public void Unmount()
		{
			Console.WriteLine("Drive.Unmount(): Device '{0}'", mDev);
			CmdWrapper	cmd = new CmdWrapper("/bin/umount " + mDev);
			cmd.MergeStderr = true;
			string		[]output = cmd.Run();
			
			foreach (string s in output)
				Console.WriteLine("MediaChanger.UnmountMedia(): DBG '{0}'", s);
			mState = DriveState.Idle;
		}
		
		/*
		 * Public Properties
		 */
		
		public DriveState State
		{
			get { return mState; }
		}
		
		public string Dev
		{
			get { return mDev; }
		}
		
		public string MountPoint
		{
			get { return mMountPoint; }
		}
		
		public bool Mounted
		{
			get { return mMounted; }
		}
	}  // END CLASS
}  // END NAMESPACE
